'use strict';
var _ = require('lodash');
exports = module.exports = function($scope, $timeout) {
    // 防抖 多次触发事件后，事件处理函数只执行一次，并且是在触发操作结束时执行
    // 场景 输入某个字符 要求实时匹配数据库 但是用户不定时会继续输入
    // demo var aa = document.getElementsByTagName('button')[0];
    // ---- aa.onclick = debounce(fun, 200);
    function debounce(method, delay) {
        let timer = null;
        return function() {
            let self = this,
                args = arguments;
            timer && clearTimeout(timer);
            timer = setTimeout(function() {
                method.apply(self, args);
            }, delay);
        }
    }
    // 节流 触发函数事件后，短时间间隔内无法连续调用，只有上一次函数执行后
    // 过了规定的时间间隔，才能进行下一次的函数调用
    function throttle(method, mustRunDelay) {
        let timer,
            args = arguments,
            start;
        return function loop() {
            let self = this;
            let now = Date.now();
            if (!start) { start = now; }
            else if (now - start < mustRunDelay) { return;}
            method.apply(self, args);
            start = now;
        }
    }
    $scope.obj20 = "啦啦|"
    $scope.fun21 = debounce(function(){
        $timeout(function(){ $scope.obj20 += "啦啦|" },0)
    }, 2e3)
    $scope.fun22 = throttle(function(){
        $timeout(function(){ $scope.obj20 += "啦啦|" },0)
    }, 2e3)
    /////////////////////////
    $scope.obj30 = "啦啦|"
    $scope.fun31 = _.debounce(() => {
        $timeout(function(){ $scope.obj30 += "啦啦|" },0)
    }, 2e3)
    $scope.fun32 = _.throttle(() => {
        $timeout(function(){ $scope.obj30 += "啦啦|" },0)
    }, 2e3, {'trailing':false})
}